package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.dao.TRoleMenuDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



/**
 * 菜单管理 服务层
 */
@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;

    @Autowired
    private TRoleMenuDao tRoleMenuDao;

    /**
     * 分页查询
     * @param queryPageBean 分页查询的条件
     * @return 返回当前页的所有菜单数据
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        String queryString = queryPageBean.getQueryString();
        //设置查询页码和每页大小
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Menu> menuPage = menuDao.findPage(queryString);
        return new PageResult(menuPage.getTotal(), menuPage.getResult());
    }

    /**
     * 添加菜单
     * @param menu
     * @return
     */
    @Override
    public boolean add(Menu menu) {
        int count = menuDao.add(menu);
        return count > 0;
    }

    /**
     * 根据 id 查询菜单
     * @param id 菜单id
     * @return
     */
    @Override
    public Menu findById(Integer id) {
        return menuDao.findById(id);
    }

    /**
     * 根据 id 删除菜单
     * @param id 菜单id
     * @return
     */
    @Override
    public boolean delete(Integer id) {
        //先判断待删除菜单是否有子级菜单, 如果有,则不允许删除
        int count = menuDao.findByParentMenuId(id); // count 就是子级菜单的数量
        if (count > 0) {
            throw new RuntimeException("此菜单存在子级菜单,不能删除");
        }
        //没有子级菜单,可以删除
        int rsCount = menuDao.delete(id);
        return rsCount > 0;
    }

    /**
     * 编辑菜单
     * @param menu
     * @return
     */
    @Override
    public boolean edit(Menu menu) {
        int count = menuDao.edit(menu);
        return count > 0;
    }

    /**
     * 查询所有菜单
     * @return
     */
    @Override
    public List<Menu> findAll() {
        return menuDao.findAll();
    }

    /**
     * 根据角色id获取该角色关联的所有菜单项id
     * @param roleId 角色id
     * @return
     */
    @Override
    public List<Integer> findByRoleId(Integer roleId) {
        return tRoleMenuDao.findMenuIdsByRoleId(roleId);
    }



    //根据登录名查询菜单
    @Override
    public List<Map<String, Object>> findMenuByUserName(String username) {
        //查询菜单
        List<Menu> menus = menuDao.findMenuByUserName(username);
        //返回结果
        List<Map<String,Object>> rsMenus = new ArrayList<>();
        //封装结果
        for (Integer i = 1;i <= menus.size();i++){
            for (Menu menu : menus) {
                if(i.toString().equals(menu.getPath())){
                    final String name = menu.getName();
                    final String path = menu.getPath();
                    final String icon = menu.getIcon();
                    List<Map<String,Object>> list = new ArrayList<>();

                    for (Integer j = 1;j <= menus.size();j++){
                        for (Menu menu1 : menus) {
                            final String path1 = menu1.getPath();
                            String path2 = "/" + i + "-" + j;
                            if (path1.equals(path2)){
                                final String name1 = menu1.getName();
                                final String linkUrl = menu1.getLinkUrl();
                                List<Map<String,Object>> list1 = new ArrayList<>();
                                Map<String,Object> map = new HashMap<>();
                                map.put("path",path1);
                                map.put("title",name1);
                                map.put("linkUrl",linkUrl);
                                map.put("children",list1);
                                list.add(map);
                            }

                        }
                    }
                    Map<String,Object> map = new HashMap<>();
                    map.put("path",path);
                    map.put("title",name);
                    map.put("icon",icon);
                    map.put("children",list);
                    rsMenus.add(map);
                }
            }
        }
        return rsMenus;
    }
}
